package cn.lxg.controller.admin;

import cn.lxg.pojo.TComment;
import cn.lxg.pojo.TUser;
import cn.lxg.service.ICommentService;
import cn.lxg.service.IUserService;
import cn.lxg.vo.CommentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller(value = "adminCommentController")
@RequestMapping(value = "/page/admin")
public class CommentController {

    @Autowired
    private final ICommentService commentService = null;

    @GetMapping("/comment_list")
    public String listPage(
            Model model,
            @RequestParam(value = "limit", required = false, defaultValue = "15") Integer limit,
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            CommentVO comment) {
        // 预处理参数
        if (comment.getContent() != null)
            comment.setContent(comment.getContent().trim());
//        if (comment.getAccount() != null)
//            comment.setAccount(comment.getAccount().trim());

        // 准备数据
        List<CommentVO> commentList = this.commentService.getList(limit, page, comment);
        int dataCount = this.commentService.countDataLine(comment);
        int pageIndex = page;
        int pageCount = (int) Math.ceil((float) dataCount / limit);

        // 传递数据
        model.addAttribute("comment", comment);
        model.addAttribute("commentList", commentList);
        model.addAttribute("dataCount", dataCount);
        model.addAttribute("defaultLimit", limit);
        model.addAttribute("pageIndex", pageIndex);
        model.addAttribute("pageCount", pageCount);

        return "admin/comment/list";
    }


    @PostMapping(value = "/comment")
    public String add(Model model, CommentVO comment) {
        // 参数预处理
//        user.setName(user.getName().trim());
//        user.setAccount(user.getAccount().trim());

        // 准备数据
        String msg = this.commentService.add(comment);

        // 传输数据给前端
        model.addAttribute("msg", msg);

        return "common/message";
    }

    @GetMapping(value = "/delete_comment/{id}")
    public String deletePage(Model model, @PathVariable(value = "id") Integer id) {
        // 准备数据
        CommentVO comment = this.commentService.get(id);

        // 传递数据
        model.addAttribute("comment", comment);

        return "admin/comment/delete";
    }

    @DeleteMapping(value = "/comment/{id}")
    public String delete(Model model, @PathVariable(value = "id") Integer id) {
        // 准备数据
        String msg = this.commentService.delete(id);

        // 传输数据给前端
        model.addAttribute("msg", msg);

        return "common/message";
    }

    @GetMapping(value = "/edit_comment/{id}")
    public String editPage(Model model, @PathVariable(value = "id") Integer id) {
        // 准备数据
        CommentVO comment = this.commentService.get(id);

        // 传输数据给前端
        model.addAttribute("comment", comment);

        return "admin/comment/edit";
    }

    @PutMapping(value = "/comment")
    public String edit(Model model, TComment comment) {
        // 参数预处理
//        user.setName(user.getName().trim());
//        user.setAccount(user.getAccount().trim());

        // 准备数据
        String msg = this.commentService.edit(comment);

        // 传输数据给前端
        model.addAttribute("msg", msg);

        return "common/message";
    }

    @GetMapping(value = "/look_comment/{id}")
    public String lookPage(Model model, @PathVariable(value = "id") Integer id) {
        // 准备数据
        CommentVO comment = this.commentService.get(id);

        // 传输数据给前端
        model.addAttribute("comment", comment);

        return "admin/comment/look";
    }
}
